{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Python基础语法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1、Python编程模式\n",
    "**交互式:**\n",
    "\n",
    "命令逐条运行\n",
    "\n",
    "交互式编程不需要创建脚本文件，是通过 Python 解释器的交互模式进来编写代码。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world\n"
     ]
    }
   ],
   "source": [
    "print(\"hello world\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    ">>> 1+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    ">>> a=1\n",
    ">>> b=1\n",
    ">>> a+b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**脚本式:**\n",
    "\n",
    "一次性全部运行\n",
    "\n",
    "通过脚本参数调用解释器开始执行脚本，直到脚本执行完毕。当脚本执行完成后，解释器不再有效。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello,world!!! I want to be a quantitative trader.\n",
      "I want to be a quantitative trader. What can I do?\n"
     ]
    }
   ],
   "source": [
    "my_dream='I want to be a quantitative trader.'\n",
    "print('Hello,world!!! '+my_dream)\n",
    "print (my_dream + ' What can I do?')\n",
    "#点击run,显示运行结果:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2、 Python标识符\n",
    "\n",
    "在 Python 里，标识符由字母、数字、下划线组成。\n",
    "\n",
    "在 Python 中，所有标识符可以包括英文、数字以及下划线_，但不能以数字开头。\n",
    "\n",
    "Python 中的标识符是区分大小写的。\n",
    "\n",
    "Python 可以同一行显示多条语句，方法是用分号 ;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-8-837b4d511801>, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-8-837b4d511801>\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m    1A_ = 1\u001b[0m\n\u001b[1;37m      ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "1A_ = 1\n",
    "print(1A_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    }
   ],
   "source": [
    "A1_ = 1\n",
    "print(A1_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "a1_ = 2\n",
    "print(a1_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "A = 1; a=2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 2\n"
     ]
    }
   ],
   "source": [
    "print(A,a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3、 Python 保留字符\n",
    "下面的列表显示了在Python中的保留字。这些保留字不能用作常数或变数，或任何其他标识符名称。\n",
    "\n",
    "所有 Python 的关键字只包含小写字母。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "|||\n",
    "|:-----:|:-----:|:-----:|\n",
    "|and|exec|not|\n",
    "|assert|finally|or|\n",
    "|break|for|pass|\n",
    "|class|from|print|\n",
    "|continue|global|raise|\n",
    "|def|if|return|\n",
    "|del|import|try|\n",
    "|elif|in|while|\n",
    "|else|is|with|\n",
    "|except|lambda|yield|"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4、代码缩进问题:\n",
    "\n",
    "缩进替代({})\n",
    "\n",
    "学习Python与其他语言最大的区别就是，Python的代码块不使用大括号（{}）来控制类，函数以及其他逻辑判断。 \n",
    "python最具特色的就是用缩进来写模块。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 代码缩进（错误）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "price = 1\n",
    "if price>0:\n",
    "    print ('True')\n",
    "        print ('price is greater than 0')\n",
    "        # 没有严格缩进，在执行时会报错\n",
    "else:\n",
    "    print (\"False\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 代码缩进（正确）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "price = 1\n",
    "if price>0:\n",
    "    print ('True')\n",
    "    print ('price is greater than 0')\n",
    "else:\n",
    "    print (\"False\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4、 多行语句处理\n",
    "Python语句中一般以新行作为语句的结束符。\n",
    "\n",
    "但是我们可以使用斜杠（ \\）将一行的语句分为多行显示，如下所示："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "Price = 1+\\\n",
    "        2+\\\n",
    "        3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n"
     ]
    }
   ],
   "source": [
    "print(Price)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5.Python 引号\n",
    "Python 可以使用引号( ' )、双引号( \" )、三引号( ''' 或 \"\"\" ) 来表示字符串，引号的开始与结束必须的相同类型的。\n",
    "\n",
    "其中三引号可以由多行组成，被当做注释。 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Channel is Quantitative Trader who like Poppin\n"
     ]
    }
   ],
   "source": [
    "Name = 'Channel'\n",
    "Career = \"Quantitative Trader\"\n",
    "Habit = '''Poppin'''\n",
    "\n",
    "print(Name+\" is \"+Career+' who like '+Habit)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "被注释\n"
     ]
    }
   ],
   "source": [
    "def annotation():\n",
    "    \"\"\"\n",
    "    Channel is Quantitative \n",
    "    Trader who like Poppin\n",
    "    \"\"\"\n",
    "    annotated = \"\"\"被注释\"\"\"\n",
    "    return annotated\n",
    "print(annotation())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 6、注释代码\n",
    "\n",
    "单行注释用 # 开头\n",
    "\n",
    "多行注释用三个引号(''')\n",
    "\n",
    "代码注释有一个功能在于可以在写代码时跳过不想运行的行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 宽客你好\n",
    "# print('hello quant')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello quant\n"
     ]
    }
   ],
   "source": [
    "# 宽客你好\n",
    "print('hello quant')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello quant\n"
     ]
    }
   ],
   "source": [
    "print('hello quant') # 宽客你好"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "OK, Let's code\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "宽客你好\n",
    "print('hello quant')\n",
    "\"\"\"\n",
    "\n",
    "print(\"OK, Let's code\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
